package com.qf.controller;

import com.google.gson.Gson;
import com.qf.pojo.BaseResult;
import com.qf.pojo.User;
import com.qf.serivce.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@Controller
@RequestMapping("/user")
public class userController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public void login(String tel , String password, HttpSession session,HttpServletResponse response){


        User user=userService.findUserByTel(tel);
        BaseResult baseResult = new BaseResult();
        if(password.equals(user.getPassword())){

            try {
                PrintWriter writer = response.getWriter();
                writer.print("{\"code\":1}");
                writer.flush();
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        else {

            PrintWriter writer = null;
            try {
                writer = response.getWriter();
            } catch (IOException e) {
                e.printStackTrace();
            }
            writer.print("{\"code\":0}");
            writer.flush();
            writer.close();
        }

    }
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public void registerAll(User user, HttpServletResponse response) throws Exception {
        boolean b = userService.insertService(user);
        PrintWriter writer = response.getWriter();
        if (b){
            writer.write("{\"code\":1}");

        }else {
            writer.write("{\"code\":0}");
        }
        writer.flush();
        writer.close();
    }
}
